from infra.handler import BaseHandler
from common.log import logInfo
from common.jwt import create_jwt_token
from common.log import logInfo
from .fav_db import create_fav
from .fav_db import delete_fav
from .fav_db import get_user_favs
import json
class FavHandler(BaseHandler):
    async def post(self):
        """添加用户喜欢的活动"""
        try:
            user_id, _ = self.get_current_user()
            if not user_id:
                self.write(json.dumps({"code": 404, "msg": "User not found"}))
                return
            data = json.loads(self.request.body)
            activity_id = data.get('activity_id', None)
            if not activity_id:
                self.write(json.dumps({"code": 404, "msg": "缺少活动ID"}))
                return
            # 调用数据库函数添加收藏
            success, result = await create_fav({
                'user_id': user_id,
                'activity_id': activity_id
            })
            
            if success:
                self.write(json.dumps({"code": 200, "msg": "添加收藏成功"}))
            else:
                self.write(json.dumps({"code": 404, "msg": result}))
                
        except Exception as e:
            logInfo(f"添加收藏失败: {str(e)}")
            self.write(json.dumps({"code": 404, "msg": str(e)}))
    
    async def get(self):
        """获取用户喜欢的活动列表"""
        try:
            user_id, _ = self.get_current_user()
            if not user_id:
                self.write(json.dumps({"code": 404, "msg": "User not found"}))
                return
            # 获取分页参数
            page = int(self.get_argument('page', 1))
            page_size = int(self.get_argument('page_size', 10))
            
            # 获取筛选参数
            filter_type = self.get_argument('type', None)
            filter_area = self.get_argument('area', None)
            filter_people = self.get_argument('people', None)
            
            # 构建筛选条件
            filters = {}
            if filter_type and filter_type != 'all':
                filters['type'] = filter_type
            if filter_area and filter_area != 'all':
                filters['area'] = filter_area
            if filter_people and filter_people != 'all':
                filters['people'] = filter_people
            
            # 调用数据库函数获取收藏列表
            success, result = await get_user_favs(user_id, page, page_size, filters)
            
            if success:
                self.write(json.dumps({"code": 200, "msg": result}))
            else:
                self.write(json.dumps({"code": 400, "msg": result}))
        except Exception as e:
            logInfo(f"获取收藏列表失败: {str(e)}")
            self.write(json.dumps({"code": 400, "msg": "获取收藏列表失败"}))
            
    async def delete(self):
        """取消收藏"""
        try:
            user_id, _ = self.get_current_user()
            if not user_id:
                self.write(json.dumps({"code": 404, "msg": "User not found"}))
                return
            # 获取请求数据
            body = json.loads(self.request.body)
           
            activity_id = body.get('activity_id', None)
         

            if not activity_id:
                self.write(json.dumps({"code": 404, "msg": "缺少活动ID"}))
                return
            # 调用数据库函数取消收藏
   
            success, result = await delete_fav(user_id, activity_id)
    
            if success:
                self.write(json.dumps({"code": 200, "msg": "取消收藏成功"}))
            else:
               
                self.write(json.dumps({"code": 404, "msg": result}))
                
        except Exception as e:
            logInfo(f"取消收藏失败: {str(e)}")
            self.write(json.dumps({"code": 404, "msg": "取消收藏失败"}))